package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.domain.vo.UserInfoVo;
import com.itheima.domain.vo.UserLikeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserManager userManager;

    //保存用户,返回主键
    @PostMapping("/save")
    public Long save(@RequestBody User user) {
        //System.out.println("save");
        return userManager.save(user);
    }

    //根据手机号查询用户
    @GetMapping("/findByPhone")
    public User findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    //发送验证码
    @PostMapping("/user/login")
    public void sendSms(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        //System.out.println(phone);
        userManager.sendSms(phone);
    }

    //登录或者注册
    @PostMapping("/user/loginVerification")
    public Map regOrLogin(@RequestBody Map<String, String> map) {
        //获取map中的手机号
        String phone = map.get("phone");
        //获取map中的验证码
        String verificationCode = map.get("verificationCode");
        //调用manager方法
        Map resultMap = userManager.regOrLogin(phone, verificationCode);
        return resultMap;
    }

    //完善个人信息
    @PostMapping("/user/loginReginfo")
    public void loginReginfo(@RequestBody UserInfo userInfo) {
        //获取请求用户对象
        User user = UserHolder.get();
        userInfo.setId(user.getId());
        userManager.saveUserInfo(userInfo);
    }

    //上传头像
    @PostMapping({"/user/loginReginfo/head", "/users/header"})
    public void uploadHeader(MultipartFile headPhoto) throws IOException {
        //System.out.println("上传头像");
        User user = UserHolder.get();
        userManager.uploadHeader(headPhoto, user.getId());
    }

    //查询个人信息
    @GetMapping("/users")
    public UserInfoVo findByUser(Long userId, Long huanxinID) {
        Long id = UserHolder.getId();
        UserInfoVo userInfoVo = null;
        if (huanxinID != null) {
            userInfoVo = userManager.findByUser(huanxinID);
        } else if (id != null) {
            userInfoVo = userManager.findByUser(id);
        } else if (userId != null) {
            userInfoVo = userManager.findByUser(userId);
        }
        //System.out.println(userInfoVo);
        return userInfoVo;
    }

    //修改个人信息
    @PutMapping("/users")
    public void updateUserInfo(@RequestBody UserInfo userInfo) {
        //获取请求的user对象信息
        User user = UserHolder.get();
        userInfo.setId(user.getId());
        userManager.updateUserInfo(userInfo);
    }

    //查询用户通用设置
    @GetMapping("/users/settings")
    public SettingVo findSettings() {
        User user = UserHolder.get();
        return userManager.findSettings(user);
    }

    /**
     * 查询用户相互喜欢/喜欢/粉丝数
     * @return
     */
    @GetMapping("/users/counts")
    public UserLikeCountVo userLikeCount() {
        return userManager.userLikeCount();
    }

    /**
     * 查询相互喜欢/喜欢/粉丝/谁看过我详细信息
     * @param page 页码
     * @param pagesize 页面大小
     * @param type 操作类型
     * @return
     */
    @GetMapping("/users/friends/{type}")
    public PageBeanVo findUserInfoByType(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            @PathVariable String type
    ) {
        return userManager.finUserInfoByType(page,pagesize,type);
    }

    @PostMapping("/users/fans/{fanId}")
    public void saveLikeFan(@PathVariable Long fanId) {
        userManager.saveLikeFan(fanId);
    }

    /**
     * 取消喜欢用户
     * @param userId 用户id
     */
    @DeleteMapping("/users/like/{userId}")
    public void deleteLikeUser(@PathVariable Long userId) {
        userManager.deleteLikeUser(userId);
    }
}
